#include "boundingbox.h"

BoundingBox::BoundingBox()
{
}

BoundingBox::BoundingBox(Vec3f min, Vec3f max)
{
    SetVec3f(this->min, min);
    SetVec3f(this->max, max);
}

BoundingBox::BoundingBox(const BoundingBox& in)
{
    SetVec3f(min, in.min);
    SetVec3f(max, in.max);
}

BoundingBox& BoundingBox::operator=(const BoundingBox& in)
{
    SetVec3f(min, in.min);
    SetVec3f(max, in.max);
    return *this;
}

void BoundingBox::getCenter(Vec3f out)
{
	AddVec3f(out, min, max);
	ScaleVec3f(out, out, 0.5f);
}

void BoundingBox::getLeftCenter(Vec3f out)
{
	getCenter(out);
	out[0] = min[0];
}

void BoundingBox::getRightCenter(Vec3f out)
{
	getCenter(out);
	out[0] = max[0];
}

void BoundingBox::merge(const BoundingBox& in)
{
    if (in.min[0] < min[0])
        min[0] = in.min[0];
    if (in.min[1] < min[1])
        min[1] = in.min[1];
    if (in.min[2] < min[2])
        min[2] = in.min[2];
    
    if (in.max[0] > max[0])
        max[0] = in.max[0];
    if (in.max[1] > max[1])
        max[1] = in.max[1];
    if (in.max[2] > max[2])
        max[2] = in.max[2];
}

void BoundingBox::translate(Vec3f displacement)
{
	AddVec3f(min, min, displacement);
	AddVec3f(max, max, displacement);
}

void BoundingBox::scale(Vec3f scaleFactors)
{
	Vec3f center;
	getCenter(center);
	
	SubtractVec3f(min, min, center);
	ComponentScaleVec3f(min, min, scaleFactors);
	AddVec3f(min, min, center);

	SubtractVec3f(max, max, center);
	ComponentScaleVec3f(max, max, scaleFactors);
	AddVec3f(max, max, center);
}

void BoundingBox::inset(Vec3f in)
{
	AddVec3f(min, min, in);
	SubtractVec3f(max, max, in);
}

void BoundingBox::inset(float x, float y, float z)
{
	Vec3f in = {x, y, z};
	inset(in);
}
